#ifndef TCPGAMEMANAGER_H
#define TCPGAMEMANAGER_H

#include <QTcpSocket>

#include "ChatWidget.h"
#include "Network.h"
#include "GameManager.h"

#define BASE_CONVERSION 16
#define PACKET_SIZE 1

class TCPGameManager : public GameManager {

    Q_OBJECT

    typedef enum RequestState {
        NONE = -1,
        MAKING_REQUEST,
        RECEIVED_REQUEST
    } RequestState;

public:
    explicit TCPGameManager(QObject *parent = 0, QTcpSocket * clientSocket = NULL, ConnectFour::Piece piece = ConnectFour::NONE, ChatWidget const * chatWidget = NULL);
    void newGame();
    void requestNewGame();
    void requestUndo();

    QTcpSocket * socket;

signals:
    void connectingNew();
    void chatReceived(QString chat);
    void socketMessageReceived(Network::SocketMessage message);
    void responseReceived(Network::SocketMessage message);
    void requested(Network::SocketMessage message);
    void winPlayer(bool player);

public slots:
    void requestMove(int index);
    void addPieceToColumn(int index);
    void finishAnimation(Animation::AnimationMode mode);

private slots:
    void socketMessage();
    void sendResponse(Network::SocketMessage message);
    void sendChat(QString chat);
    void determineWinner(ConnectFour::Piece piece);

private:
    void connectSignalSlots(ChatWidget const * chatWidget);
    void addOpponentPieceToColumn(int index);
    void newGameRequested();
    void undoPlayerMove();
    void undoOpponentMove();
    void undoRequested();

    ConnectFour::Piece startingPiece;
    ConnectFour::Piece playerPiece;
    int previousOpponentMoveIndex;
    RequestState requestState;
    Animation::AnimationMode queueOpponentMove;
};

#endif // TCPGAMEMANAGER_H
